import React from "react";
import { Link, Routes, Route, Navigate } from "react-router-dom";

const useAuth = () => {
  return !!localStorage.getItem("token");
};

const Home = () => {
  return <div>home页面</div>;
};
const About = () => {
  return <div>about页面</div>;
};

const RequireLogin = (props) => {
  // todo 检查登录态，是可以封装成自定义hooks
  const done = useAuth();
  if (done) {
    return props.children;
  } else {
    return <Navigate to="/login"></Navigate>;
  }
};

const App = () => {
  return (
    <>
      <h2>router-v6-auth</h2>
      <ul>
        <li>
          <Link to="/home">home</Link>
        </li>
        <li>
          <Link to="/about">about</Link>
        </li>
      </ul>

      <hr />

      <Routes>
        {/* Navigate */}
        <Route path="/" element={<Navigate to="/home"></Navigate>}></Route>
        <Route path="/home" element={<Home />}></Route>
        <Route
          path="/about"
          element={
            <RequireLogin>
              <About />
            </RequireLogin>
          }
        ></Route>
      </Routes>
    </>
  );
};

export default App;
